import router from "./router"



import loginApi from "./api/login"

//通过路由对象我们可以调用导航守卫方法
router.beforeEach((to, from, next) => {
    //获取token
    const token = localStorage.getItem("adminToken");
    //没有token
    if(!token){
        if(to.path !== "/login"){
            next({path : "/login"})
        }else{
            next();
        }
    }else{ //有token的情况
        if(to.path == "/login"){
            next();
        }else{
            //获取用户信息
            const userInfo = localStorage.getItem("adminInfo");
            if(userInfo){
                next();
            }else{
                loginApi.getInfo(token).then(response=>{
                    const resp = response.data;
                    console.log(resp)
                    if(resp.flag){
                        localStorage.setItem("adminInfo",JSON.stringify(resp.data));
                        next();
                    }else{
                        next({path : "/login"})
                    }
                })
            }
        }
    }
})